<template>
  <el-container class="addStock-page">
    <el-main>
      <el-form ref="addStockRef" :model="form.model" :rules="form.rules" label-width="84px">
        <el-form-item label="商品库存:" prop="stock">
          <div class="sa-flex">
            <span class="stock">{{ modal.params.row.stock }}</span>
            <el-input v-model="form.model.stock" placeholder="补充库存" type="number">
              <template #append>件</template>
            </el-input>
          </div>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button type="primary" @click="confirm">确定</el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
  import { getCurrentInstance, reactive, unref, ref } from 'vue';
  import { api } from '../goods.service';

  const emit = defineEmits(['modalCallBack']);
  const props = defineProps(['modal']);
  const addStockRef = ref(null);
  const { proxy } = getCurrentInstance();

  const form = reactive({
    model: {
      stock: '',
    },
    rules: {
      stock: [{ required: true, message: '请输入补充库存', trigger: 'blur' }],
    },
  });

  function confirm() {
    unref(addStockRef).validate(async (valid) => {
      if (!valid) return;
      if (valid) {
        await api.stockWarning.addStock(props.modal.params.row.id, {
          stock: form.model.stock,
        });
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }
</script>

<style lang="scss">
  .sa-dialog.addStock-modal {
    --el-dialog-width: 400px;
  }
</style>
<style lang="scss" scoped>
  .addStock-page {
    .stock {
      flex-shrink: 0;
      margin-right: 16px;
      color: var(--sa-font);
      font-size: 12px;
    }
  }
</style>
